﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Threading;

public class MailHelper
{
    private static string gMailAccount { get { return System.Configuration.ConfigurationManager.AppSettings["mail"]; } }
    private static string password { get { return System.Configuration.ConfigurationManager.AppSettings["mailpassword"]; } }

    public static string Domain { get { return System.Configuration.ConfigurationManager.AppSettings["domain"]; } }
    public static string Port { get { return System.Configuration.ConfigurationManager.AppSettings["port"]; } }
    /// <summary>
    /// This method use a Thread for sending Email(Gmail or extend gmail only) (Leo)
    /// </summary>
    /// <param name="gMailAccount"></param>
    /// <param name="password"></param>
    /// <param name="to"></param>
    /// <param name="subject"></param>
    /// <param name="message"></param>
    /// <returns></returns>
    public static string SendMail(string to, string subject, string message)
    {
        try
        {
            NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);

            MailMessage mm = new MailMessage(gMailAccount, to);

            mm.Sender = new MailAddress(gMailAccount);
            mm.Subject = subject;
            mm.Body = message;
            mm.IsBodyHtml = true;
            SmtpClient smtp;
            if (Port != "-1")
                smtp = new SmtpClient(Domain, Int32.Parse(Port));
            else
                smtp = new SmtpClient(Domain);
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = loginInfo;

            smtp.Send(mm);

            return "true";
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }

    }

    public static string SendMail1(string to, string subject, string message, string MailAccount,string MK)
    {
        try
        {
            NetworkCredential loginInfo = new NetworkCredential(MailAccount, MK);

            MailMessage mm = new MailMessage(MailAccount, to);

            mm.Sender = new MailAddress(MailAccount);
            mm.Subject = subject;
            mm.Body = message;
            mm.IsBodyHtml = true;
            SmtpClient smtp;
            if (Port != "-1")
                smtp = new SmtpClient(Domain, Int32.Parse(Port));
            else
                smtp = new SmtpClient(Domain);
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = loginInfo;

            smtp.Send(mm);

            return "true";
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }

    }
    /// <summary>
    /// This method use when you want to send some Emails ( A expansion of SendMail ) (Leo)
    /// </summary>
    /// <param name="gMailAccount"></param>
    /// <param name="password"></param>
    /// <param name="to"></param>
    /// <param name="subject"></param>
    /// <param name="message"></param>
    /// <returns></returns>
    /// 
    public static bool SendMail(string[] to, string subject, string message)
    {
        try
        {
            new Thread(() =>
            {
                NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(gMailAccount);
                foreach (string s in to)
                {
                    msg.To.Add(new MailAddress(s));
                }
                msg.Subject = subject;
                msg.Body = message;
                msg.IsBodyHtml = true;
                SmtpClient client = new SmtpClient("smtp.gmail.com");
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = loginInfo;
                client.Send(msg);
            }).Start();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
    public static bool SendMail1(string[] to, string subject, string message, string MailAccount, string MK)
    {
        try
        {
            new Thread(() =>
            {
                NetworkCredential loginInfo = new NetworkCredential(MailAccount, MK);
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(MailAccount);
                foreach (string s in to)
                {
                    msg.To.Add(new MailAddress(s));
                }
                msg.Subject = subject;
                msg.Body = message;
                msg.IsBodyHtml = true;
                SmtpClient client = new SmtpClient("smtp.gmail.com");
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = loginInfo;
                client.Send(msg);
            }).Start();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
}